//
//  GYUserInfo.m
//  Visitor
//
//  Created by x on 2017/9/28.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYUserInfo.h"
#import "NSString+FilePath.h"
#import "GYNetWorkTools.h"

@implementation GYUserInfo

- (instancetype)initWithDic:(NSDictionary *)dic {
    if ([super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return  self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

- (void)saveUserInfo {
    [NSKeyedArchiver archiveRootObject:self toFile:[NSString CachesDir:@"userInfo.plist"]];
}

static GYUserInfo *_userInfo;

+ (GYUserInfo *)loadUserInfo {
//    //1.首先检测这个地址文件存不存在
    BOOL isExistence = [[NSFileManager defaultManager]isExecutableFileAtPath:[NSString CachesDir:@"userInfo.plist"]];
    
    if (_userInfo != nil && isExistence) {
        return _userInfo;
    }
    
    //解档
    _userInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString CachesDir:@"userInfo.plist"]];
    
    return _userInfo;
}

+ (BOOL)removeUserInfo {
    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath:[NSString CachesDir:@"userInfo.plist"] error:&error];
    if (error == nil) {
        NSLog(@"删除信息没错误...");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loginOutSuccess" object:nil];
        return YES;
    }else {
        NSLog(@"删除个人信息失败....====%@",error);
        return NO;
    }
}


- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_account forKey:@"account"];
    [aCoder encodeInt:_id forKey:@"id"];
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeObject:_organizationName forKey:@"organizationName"];
    [aCoder encodeObject:_phone forKey:@"phone"];
    [aCoder encodeObject:_requestKey forKey:@"requestKey"];
    [aCoder encodeInt:_status forKey:@"status"];
    
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _account = [aDecoder decodeObjectForKey:@"account"];
        _id = [aDecoder decodeIntForKey:@"id"];
        _name = [aDecoder decodeObjectForKey:@"name"];
        _organizationName = [aDecoder decodeObjectForKey:@"organizationName"];
        _phone = [aDecoder decodeObjectForKey:@"organizationName"];
        _requestKey = [aDecoder decodeObjectForKey:@"requestKey"];
        _status = [aDecoder decodeIntForKey:@"status"];
    }
    return self;
}


- (NSString *)description {
    NSArray *keys = @[@"account",@"id",@"name",@"organizationName",@"phone",@"requestKey",@"status"];
    return [NSString stringWithFormat:@"%@",[self dictionaryWithValuesForKeys:keys]];
}


@end
